package com.jysz.certificate.web.action;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRExporterParameter;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;

import com.jysz.certificate.action.AbstractBaseAction;

/**
 * @author kacarich email:javaengine@outlook.com
 *
 */
@ParentPackage("jr-package")
public abstract class AbstractJasperReportAction extends AbstractBaseAction {
	
	private Connection connection;
	private String documentName;
	private String jasperFileMap;
	private Map<JRExporterParameter, Object> exportParameters;
	
	/**
	 * @return the jasperFileMap
	 */
	public String getJasperFileMap() {
		return jasperFileMap;
	}

	/**
	 * @param jasperFileMap the jasperFileMap to set
	 */
	public void setJasperFileMap(String jasperFileMap) {
		this.jasperFileMap = jasperFileMap;
	}

	/**
	 * @return the documentName
	 */
	public String getDocumentName() {
		return documentName;
	}

	/**
	 * @param documentName the documentName to set
	 */
	public void setDocumentName(String documentName) {
		this.documentName = documentName;
	}

	private Map<String,String> para;

	private List<String> fillMap;
	/**
	 * @return the fillMap
	 */
	public List<String> getFillMap() {
		return fillMap;
	}

	/**
	 * @param fillMap the fillMap to set
	 */
	public void setFillMap(List<String> fillMap) {
		this.fillMap = fillMap;
	}

	@SuppressWarnings("rawtypes")
	private Map paras;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String execute() throws Exception {
        return SUCCESS;
    }
	
	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Action(value="output_*_*", results={@Result(name = SUCCESS, type="jasper",location= "/jr/{1}.jasper",params={"documentName","${documentName}","jasperFileMap","fillMap","connection", "connection", "format","{2}","reportParameters","paras", "exportParameters","exportParameters", "imageServletUrl","/jr/images?image="})})
	public String output() throws Exception {
		//FIXED:这个location不支持convertion插件呈现视图..有时间重写下让他支持.
		this.connection =  getDataSource().getConnection();
		if(para != null){
			if(paras == null){
				paras = new HashMap();
			}
			paras.putAll(para);
		//org.apache.commons.codec.binary.Base64.u
		}
		
		if(exportParameters == null){
			exportParameters = new HashMap<JRExporterParameter, Object>();
		}
		
//		exportParameters.put(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
        return SUCCESS;
    }
	
	/**
	 * @return
	 */
	public abstract DataSource getDataSource();
	
	public Map<String, String> getPara() {
		return para;
	}

	public void setPara(Map<String, String> para) {
		this.para = para;
	}

	/**
	 * @return the paras
	 */
	@SuppressWarnings("rawtypes")
	public Map getParas() {
		return paras;
	}
	
	/**
	 * @param paras the paras to set
	 */
	@SuppressWarnings("rawtypes")
	public void setParas(Map paras) {
		this.paras = paras;
	}

	public Map<JRExporterParameter, Object> getExportParameters() {
		return exportParameters;
	}

	public void setExportParameters(
			Map<JRExporterParameter, Object> exportParameters) {
		this.exportParameters = exportParameters;
	}
	
	
}
